Fixed GtkToolPalette ->forall implementation to be gtk_widget_destroy friendly
authorTristan Van Berkom <tristan.van.berkom@gmail.com>
Mon, 31 Jan 2011 19:57:05 +0000 (04:57 +0900)
committerTristan Van Berkom <tristan.van.berkom@gmail.com>
Mon, 31 Jan 2011 20:02:56 +0000 (05:02 +0900)
The forall() loop was buggy as it was skipping items in the list when
the current item gets removed from the groups array as a result of
calling the callback (causing memory leaks).

gtk/gtktoolpalette.c

index fea7bf1f5ce5af30ff37a7c9014a7dda8327da57..fa5188cde0f3e9c71f4e286c9d1b0be4bc0cfaf3 100644 (file)
@@ -831,15 +831,21 @@ gtk_tool_palette_forall (GtkContainer *container,
                          gpointer      callback_data)
 {
   GtkToolPalette *palette = GTK_TOOL_PALETTE (container);
-  guint i;
-
+  guint i, len;
 
   for (i = 0; i < palette->priv->groups->len; ++i)
     {
       GtkToolItemGroupInfo *info = g_ptr_array_index (palette->priv->groups, i);
+
+      len = palette->priv->groups->len;
+
       if (info->widget)
         callback (GTK_WIDGET (info->widget),
                   callback_data);
+
+      /* At destroy time, 'callback' results in removing a widget,
+       * here we just reset the current index to account for the removed widget. */
+      i -= (len - palette->priv->groups->len);
     }
 }